<?php

header("Content-type: text/html; charset=utf-8");
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$conn = new mysqli('localhost','root','root');
//创建数据库test
$sql1 = "CREATE DATABASE test";
if ($conn->query($sql1) === TRUE) {
    echo "首次访问，数据库创建成功！！！！";
} else {
    
}

mysqli_select_db($conn,"test");
$sql1 = "CREATE TABLE user (
    id INT(6)  AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(50) NOT NULL,
    password VARCHAR(50) NOT NULL
    )";
     
    if ($conn->query($sql1) === TRUE) {
        echo "首次访问，创建user表成功！！！！";
    } 

if ($conn->connect_error){
    echo '数据库连接失败！';
    exit(0);
}else{
        if ($username == ''){
            echo '<script>alert("请输入用户名！");history.go(-1);</script>';
            exit(0);
        }
        if ($password == ''){
            echo '<script>alert("请输入密码！");history.go(-1);</script>';
            exit(0);
        }
        $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'";
        $result = $conn->query($sql);
        $number = mysqli_num_rows($result);
        if ($number) {
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;//一般情况下session中不保存密码信息
			echo '<script>alert("登录成功,关闭进入主页！！！");</script>';
            echo '<script>window.location="../index.php";</script>';
        } else {
            echo '<script>alert("用户名或密码错误！");history.go(-1);</script>';
        }
    }
?>